import React from 'react';
import router from 'umi/router';
//import Link from 'umi/link'

import { connect } from 'dva';

import { Card, Button,Modal } from 'antd';

import DescriptionList from '@/components/DescriptionList';
const { Description } = DescriptionList;

const formPath = '/HR/HrExpenseForm';
const listPath = '/HR/HrExpenses';


@connect(({ hrExpense }) => ({ hrExpense }))
class Profile extends React.Component {
  state = {
    visibleDel: false,
  }

  id = 0

  async componentDidMount() {
    const { location: {query: { id } } } = this.props;
    this.id = Number(id)
    if (this.id) {
      const { dispatch } = this.props;
      dispatch({
        type: 'hrExpense/view',
        payload:  { id: this.id }
      });
    }
  }

  compute(){
    if (this.id) {
      const { dispatch } = this.props;
      dispatch({
        type: 'hrPayslip/compute',
        payload:  { id: this.id }
      });
    }
  }

  post(){
    if (this.id) {
      const { dispatch } = this.props;
      dispatch({
        type: 'hrPayslip/post',
        payload:  { id: this.id }
      });
    }
  }

  unpost(){
    if (this.id) {
      const { dispatch } = this.props;
      dispatch({
        type: 'hrPayslip/unpost',
        payload:  { id: this.id }
      });
    }
  }


  gotoForm() {
    if(this.id){
      router.push(`${formPath}?id=${this.id}`);
    }
  }


  showDelete() {
    this.setState({
      visibleDel: true
    })

  }

  handleCancel(){
    this.setState({
      visibleDel: false,
    })
  }

  async HandleDelete(){
    if (this.id) {
      const { dispatch } = this.props;
      await dispatch({
        type: 'hrExpense/unlink',
        payload:  { id: this.id }
      });

      const { hrExpense } = this.props;
      if( hrExpense.id ){
      }
      else{
        router.push(listPath);

      }
    }
  }

  render() {
    const { hrExpense: { record } } = this.props;

    //console.log(record)

    const {  visibleDel } = this.state

    return (
      <Card bordered={false}>

        <DescriptionList size="large" title='操作控制' style={{ marginBottom: 32 }}>
          <Description >
            <Button onClick={()=>this.gotoForm()}>编辑</Button>
          </Description>
          <Description >
            <Button onClick={()=>this.showDelete()}>删除</Button>
          </Description>

          <Description >
            <Button onClick={()=>this.compute()}>计算</Button>
          </Description>

          <Description >
            <Button onClick={()=>this.post()}>确认</Button>
          </Description>

          <Description >
            <Button onClick={()=>this.unpost()}>反确认</Button>
          </Description>


        </DescriptionList>

        <Modal title='删除'
          visible={visibleDel}
          onOk={()=>this.HandleDelete()}
          onCancel={()=>this.handleCancel()}
        >
          确认删除?

        </Modal>


        <DescriptionList size="large" title='基本信息' style={{ marginBottom: 32 }}>
          <Description term='状态' >{record.state}</Description>
          <Description term='支付模式' >{record.payment_mode}</Description>
          <Description term='名称' >{record.name}</Description>
          <Description term='员工' >{record.employee_id && record.employee_id.name}</Description>
          <Description term='费用' >{record.product_id && record.product_id.name}</Description>
          <Description term='单价' >{record.unit_amount}</Description>
          <Description term='数量' >{record.quantity}</Description>
          <Description term='不含税金额' >{record.untaxed_amount}</Description>
          <Description term='总金额' >{record.total_amount}</Description>
          <Description term='科目' >{record.account_id && record.account_id.name}</Description>
          <Description term='描述' >{record.description}</Description>
          <Description term='费用报告' >{record.sheet_id && record.sheet_id.name}</Description>
          <Description term='参考' >{record.reference}</Description>
          <Description term='是否是红单' >{JSON.stringify(record.is_refused)}</Description>
        </DescriptionList>
      </Card>
    );
  }
}

export default Profile;

